/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestArrays.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package arrays;

import java.util.Arrays;
import java.util.List;

/**
 * java.util.Arrays提供了一系列操作数组的方法<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestArrays {

	/**
	 * 填充数组
	 */
	private static void fillArrays() {
		// 填充整个数组
		int[] array = new int[10];
		Arrays.fill(array, 5);
		System.out.println("填充后: " + Arrays.toString(array));

		// 填充数组的指定位置[3,8]
		Arrays.fill(array, 3, 8, 10);
		System.out.println("填充指定位置: " + Arrays.toString(array));
	}

	/**
	 * 数组排序
	 */
	private static void sortArrays() {
		int[] array = { 7, 8, 4, 5, 1, 12, 9 };
		System.out.println("排序前\t" + Arrays.toString(array));
		Arrays.sort(array);
		System.out.println("排序后\t" + Arrays.toString(array));
	}

	/**
	 * 比较数组内容
	 */
	private static void compareArrayContent() {
		char[] array1 = { 'a', 'b', 'c' };
		char[] array2 = { 'a', 'b', 'c' };
		System.out.println(Arrays.equals(array1, array2));
		System.out.println("Hash code of array1 = " + Arrays.hashCode(array1));
		System.out.println("Hash code of array2 = " + Arrays.hashCode(array2));

		// 对于char类型数组可以转换为string进行比较
		System.out.println(new String(array1).equals(new String(array2)));
	}

	/**
	 * 二分法查找[使用二分法查找的数组必须经过排序]
	 */
	private static void binarySearch() {
		System.out.println(Arrays.binarySearch(new int[] { 2, 4, 6, 8, 10 }, 4));
	}

	/**
	 * 数组拷贝
	 */
	private static void copyArray() {
		int[] array = { 1, 2, 3, 4, 5 };
		System.out.println("array = " + Arrays.toString(array));

		int[] copiedArray = new int[array.length];
		System.arraycopy(array, 1, copiedArray, 0, 4);
		System.out.println("copied array = " + Arrays.toString(copiedArray));

		// jdk 1.6的数组拷贝
		int[] jdk6CopiedArray = Arrays.copyOf(array, array.length - 1);
		System.out.println("jdk 1.6 copy array = " + Arrays.toString(jdk6CopiedArray));

		// 使用clone拷贝数组
		int[] clonedArray = array.clone();
		clonedArray[3] = 0;
		System.out.println("clonedArray = " + Arrays.toString(clonedArray));
	}

	/**
	 * 输出数组内容
	 */
	private static void arrayToString() {
		int[] array1 = { 1, 2, 3, 4, 5 };
		System.out.println("array1 = " + Arrays.toString(array1));

		int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		System.out.println("array2 = " + Arrays.deepToString(array2));
	}

	/**
	 * 将数组转换为集合
	 */
	private static void arrayToList() {
		String[] array = { "one", "two", "three" };
		List<String> list = Arrays.asList(array);
		System.out.println("将数组转换为集合: " + list);
	}

	/**
	 * 测试Arrays的方法
	 */
	public static void testArraysMethods() {
		fillArrays();
		sortArrays();
		compareArrayContent();
		binarySearch();
		copyArray();
		arrayToString();
		arrayToList();
	}

	public static void main(String[] args) {
		testArraysMethods();
	}
}
